﻿using Coldairarrow.Entity.Base_Company;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;

using EFCore.Sharding;
using LinqKit;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base_Company
{
    public class Base_ComapnyDataBusiness : BaseBusiness<Base_ComapnyData>, IBase_ComapnyDataBusiness, ITransientDependency
    {
        readonly IOperator _Operator;
        readonly IHostingEnvironment _hostingEnvironment;
        public Base_ComapnyDataBusiness(IDbAccessor db, IOperator @operator, IHostingEnvironment hostingEnvironment)
            : base(db)
        {
            _hostingEnvironment = hostingEnvironment;
            _Operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<Base_ComapnyData>> GetDataListAsync(PageInput<ComapnyDataInputDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<Base_ComapnyData>();
            var search = input.Search;

            //筛选
            if (!search.CompanyId.IsNullOrEmpty())
            {
                where = where.And(x => x.CompanyId == search.CompanyId);
            }
            if (!search.Search.IsNullOrEmpty())
            {

                where = where.And(x => x.ManagerURL.Contains(search.Search) || x.WebsiteURL.Contains(search.Search));
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<Base_ComapnyData> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public Base_ComapnyData GetModel(string id)
        {
            return  GetEntity(id);
        }

        /// <summary>
        /// 根据后台网址获取公司Id
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetCId(string url)
        {
            string cId = string.Empty;
            var q = GetIQueryable();
            var list = q.Where(x => url.Contains(x.ManagerURL) && x.State && !x.Deleted).ToList();
            if (list.Count > 0)
            {
                cId = list[0].CompanyId;
            }

            return cId;
        }

        /// <summary>
        /// 根据前台网址获取公司Id
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetFCId(string url)
        {
            string cId = string.Empty;
            var q = GetIQueryable();
            var list = q.Where(x => (url.Contains(x.WebsiteURL)) && x.State && !x.Deleted).ToList();
            if (list.Count > 0)
            {
                cId = list[0].CompanyId;
            }

            return cId;
        }

        /// <summary>
        /// 根据前台网址获取公司Id
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetTFCId(string url)
        {
            string cId = string.Empty;
            var q = GetIQueryable();
            var list = q.Where(x => (x.WebsiteURL.Contains(url)) && x.State && !x.Deleted).ToList();
            if (list.Count > 0)
            {
                cId = list[0].CompanyId;
            }

            return cId;
        }

        public async Task AddDataAsync(Base_ComapnyData data)
        {
            var q = GetIQueryable();
            var list = q.Where(x => (x.WebsiteURL == data.WebsiteURL || x.ManagerURL == data.ManagerURL) && x.Deleted == false).ToList();
            if (list.Count > 0)
            {
                throw new BusException("管理端网址或者网站网址与其他网站重复，请重新输入正确网址！");
            }
            else
            {
                await InsertAsync(data);
            } 
        }

        public async Task UpdateDataAsync(Base_ComapnyData data)
        {
            var q = GetIQueryable();
            var list = q.Where(x => (x.WebsiteURL == data.WebsiteURL || x.ManagerURL == data.ManagerURL) && x.Deleted == false && x.Id != data.Id).ToList();
            if (list.Count > 0)
            {
                throw new BusException("管理端网址或者网站网址与其他网站重复，请重新输入正确网址！");
            }
            else
            {
                await UpdateAsync(data);
            }
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

       
       

        
        public string GetMWebSite(string cId) { 
            string site = "";
            var model = GetIQueryable().Where(x => x.CompanyId == cId && x.State).OrderByDescending(x => x.CreateTime).FirstOrDefault();
            if (!model.IsNullOrEmpty())
            {
                site = model.ManagerURL;
            }

            return site;
        }
        #endregion

        #region 私有成员
     

        public List<string> GetTest()
        {
            List<string> rList = new List<string>();
            setData(rList);

            return rList;
        }

        private void setData(List<string> rList)
        {
            rList.Add("1");
            rList.Add("2");
            rList.Add("3");
        }

        public Task SetRCompanys(SetRCompanyDTO input)
        {
            throw new NotImplementedException();
        }

        public object GetRCompanys(string id)
        {
            throw new NotImplementedException();
        }

        public List<object> GetRCompanyList(string id)
        {
            throw new NotImplementedException();
        }

        public string GetFile()
        {
            throw new NotImplementedException();
        }

        private class UserInfo
        { 
           public string Name { get; set; }
           public int Age { get; set; }
           public DateTime JoinTime { get; set; }
        }

        #endregion
    }
}